//
//  LLPortalViewModel.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLPortalViewModel.h"
#import "LLMenuItem.h"

#define TAB_MAIN                        @"homePageTabMain"
#define TAB_HUFU                        @"homePageTabHufu"
#define TAB_CAIZHUANG                   @"homePageTabCaizhuang"
#define TAB_XIHU                        @"homePageTabXihu"
#define TAB_NANSHI                      @"homePageTabNanshi"

#define TAB_H5                          @"homePageTabH5"        // 这个是前缀

@interface LLPortalViewModel ()
@property(nonatomic, strong, readwrite) LLMainViewModel *mainViewModel;
@property(nonatomic, strong, readwrite) NSArray<LLViewModel *> *viewModels;
@property(nonatomic, strong, readwrite) NSArray<LLViewController *> *viewControllers;
@property(nonatomic, strong, readwrite) RACCommand *searchBtnCommand;
@property(nonatomic, strong, readwrite) RACCommand *loadSegmentsCommand;
@end

@implementation LLPortalViewModel

- (void)initialize {
    self.title = @"丽人丽妆";
    self.mainViewModel = [[LLMainViewModel alloc] initWithParams:nil];

    self.loadSegmentsCommand =
            [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {

                @weakify(self)
                void (^nextBlock)(LLMenuItemBag *) = ^(LLMenuItemBag *menuItemBag) {
                    @strongify(self)
                    self.viewModels = [[[menuItemBag.menuItems.rac_sequence.signal
                            map:^id(LLMenuItem *item) {
                                if ([TAB_MAIN isEqualToString:item.identifier]) {
                                    LLMainViewModel *vm = [[LLMainViewModel alloc] init];
                                    vm.segmentedTitle = item.name;
                                    return vm;
                                } else if ([TAB_HUFU isEqualToString:item.identifier]
                                        || [TAB_CAIZHUANG isEqualToString:item.identifier]
                                        || [TAB_HUFU isEqualToString:item.identifier]
                                        || [TAB_XIHU isEqualToString:item.identifier]
                                        || [TAB_NANSHI isEqualToString:item.identifier]) {
                                    LLMainViewModel *vm = [[LLMainViewModel alloc] init];
                                    vm.segmentedTitle = item.name;
                                    return vm;
                                } else if ([item.identifier hasPrefix:TAB_H5] && item.url.length > 0) {
                                    LLMainViewModel *vm = [[LLMainViewModel alloc] init];
                                    vm.segmentedTitle = item.name;
                                    return vm;
                                }
                                return nil;
                            }]
                            ignore:nil]
                            toArray];
                    NSMutableArray *vcs = [NSMutableArray array];
                    [self.viewModels enumerateObjectsUsingBlock:^(LLViewModel *_Nonnull vm, NSUInteger idx, BOOL *_Nonnull stop) {
                        [vcs addObject:[[LLRouter sharedInstance] viewControllerForViewModel:vm]];
                    }];

                    self.viewControllers = vcs;
                };

                NSDictionary *params = @{@"type" : @"homePageTabContent"};
                return [[[LLHTTPSessionManager shareManager]
                        request:URL_CHANNEL_AC_LIST params:params resultClass:[LLMenuItemBag class]]
                        doNext:^(LLMenuItemBag *menuItemBag) {
                            nextBlock(menuItemBag);
                        }];
            }];

    self.searchBtnCommand =
            [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
                return [RACSignal empty];
            }];
}


@end
